package com.example.tang.zhayan.utils;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.example.tang.zhayan.R;
import com.example.tang.zhayan.adapter.SearcheDesignerAdapter;
import com.example.tang.zhayan.adapter.WorksBodyAdapter;
import com.example.tang.zhayan.bean.BaseSearchDesigner;
import com.example.tang.zhayan.bean.BaseWorksBody;
import com.example.tang.zhayan.bean.SearchDesignerItem;
import com.example.tang.zhayan.bean.WorksBodyItem;
import com.example.tang.zhayan.constant.PathConstant;
import com.example.tang.zhayan.ui.BuyDetailsActivity;
import com.example.tang.zhayan.ui.DesignerActivity;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import org.xutils.common.Callback;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by tang on 2015/12/17.
 */
public class PopupwindowUtils {
    /**
     * 搜索界面的弹出框
     */
    private static int pageNo = 1;
    private static String keyWord = "";
    private static List<SearchDesignerItem> list;
    private static SearcheDesignerAdapter adapter;
    private static List<WorksBodyItem> listClothes;
    private static WorksBodyAdapter clothesAdapter;
    private static boolean flag = true;
    private static PullToRefreshListView ptrListView;
    private static ProgressDialog dialog;

    /**
     * 添加Popupwindow获取搜索界面弹出框
     */
    public static void getSearchPopupWindow(View v, View view, final Context context, final PopupWindow popupSearchWindow) {
        final Map<String, String> map = new HashMap<>();
        dialog = new ProgressDialog(context);
        dialog.setMessage(context.getString(R.string.stringIsLoading));
        if (list == null) {
            list = new ArrayList<>();
        }
        if (listClothes == null) {
            listClothes = new ArrayList<>();
        }
        if (adapter == null) {
            adapter = new SearcheDesignerAdapter(context, list);
        }
        if (clothesAdapter == null) {
            clothesAdapter = new WorksBodyAdapter(context, listClothes);
        }

        final EditText textName = (EditText) view.findViewById(R.id.et_search);
        ImageView imgBack = (ImageView) view.findViewById(R.id.iv_search_back);
        ImageView imgShop = (ImageView) view.findViewById(R.id.iv_search_shop);
        Button btnDesigner = (Button) view.findViewById(R.id.btn_search_deisgner);
        Button btnClothes = (Button) view.findViewById(R.id.btn_search_clothes);
        ptrListView = (PullToRefreshListView) view.findViewById(R.id.ptr_search);
        ptrListView.setMode(PullToRefreshListView.Mode.BOTH);
        final AbsListView.LayoutParams params = new AbsListView.LayoutParams(new WindowsUtils(context)
                .getScreenWidth(), new WindowsUtils(context).getScreenHeight());
        view.setLayoutParams(params);

        imgBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupSearchWindow.dismiss();
            }
        });
        btnClothes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //true表示为搜索衣服
                flag = true;
                map.clear();
                pageNo = 1;
                keyWord = textName.getText().toString();
                if (!keyWord.equals("")) {
                    dialog.show();
                    getClothesData(map, keyWord);
                } else {
                    Toast.makeText(context, "搜索内容不能为空", Toast
                            .LENGTH_SHORT).show();
                }
                ptrListView.setAdapter(clothesAdapter);
            }
        });

        btnDesigner.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.show();
                //false表示
                flag = false;
                pageNo = 1;
                keyWord = textName.getText().toString();
                getDesignerData(map, keyWord);
                ptrListView.setAdapter(adapter);
            }
        });

        /**
         * ptrListview的滑动事件
         */
        ptrListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                if (!flag) {
                    getDesignerData(map, keyWord);
                } else {
                    getClothesData(map, keyWord);
                }

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                pageNo++;
                if (!flag) {
                    getDesignerData(map, keyWord);
                } else {
                    getClothesData(map, keyWord);
                }

            }
        });

        /**
         * ptrListView的点击事件
         */
        ptrListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (!flag) {
                    Intent intent = new Intent(context, DesignerActivity.class);
                    intent.putExtra("designerMemberId", list.get(position).getMemberId());
                    context.startActivity(intent);
                } else {
                    Intent intent = new Intent(context, BuyDetailsActivity.class);
                    intent.putExtra("id", listClothes.get(position).getId());
                    intent.putExtra("memberId", listClothes.get(position).getStylist_id());
                    context.startActivity(intent);
                }
            }
        });

        //让Popupwindow上面的控件获取焦点
        popupSearchWindow.setFocusable(true);
        //防止其他的控件没有焦点
        popupSearchWindow.setBackgroundDrawable(new BitmapDrawable());
        popupSearchWindow.setOutsideTouchable(true);
        popupSearchWindow.setAnimationStyle(R.style.searchpopwindow_anim_style);
        //在底部显示Popupwindow
        popupSearchWindow.showAtLocation(v, Gravity.BOTTOM, 0, 0);
    }


    /**
     * 得到设计师的数据
     */
    public static void getDesignerData(Map<String, String> map, String keyWord) {
        map.clear();
        map.put("pageNo", pageNo + "");
        map.put("keyword", keyWord.trim());
        x.http().post(ResultParamsUtils.getIDParams(PathConstant.WORKS_SEARCH, map), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String s) {
                BaseSearchDesigner designer = JSON.parseObject(s, BaseSearchDesigner.class);
                list.addAll(designer.getData().getList());
                adapter.notifyDataSetChanged();
                ptrListView.onRefreshComplete();
                //如果进度条是显示的就隐藏
                if (dialog.isShowing()) {
                    dialog.dismiss();
                }
            }

            @Override
            public void onError(Throwable throwable, boolean b) {
            }

            @Override
            public void onCancelled(CancelledException e) {
            }

            @Override
            public void onFinished() {
            }
        });
    }

    /**
     * 得到设计师的数据
     */
    public static void getClothesData(Map<String, String> map, String keyWord) {
        map.clear();
        map.put("pageNo", pageNo + "");
        map.put("pageSize", "10");
        map.put("keyword", keyWord.trim());
        x.http().post(ResultParamsUtils.getIDParams(PathConstant.WORKS_SEARCH_CLOTHES, map), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String s) {
                BaseWorksBody baseWorksBody = JSON.parseObject(s, BaseWorksBody.class);
                listClothes.addAll(baseWorksBody.getData().getList());
                clothesAdapter.notifyDataSetChanged();
                ptrListView.onRefreshComplete();
                //如果进度条是显示的就隐藏
                if (dialog.isShowing()) {
                    dialog.dismiss();
                }
            }

            @Override
            public void onError(Throwable throwable, boolean b) {
            }

            @Override
            public void onCancelled(CancelledException e) {
            }

            @Override
            public void onFinished() {
            }
        });
    }

}
